package cn.com.backstage.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.ehcache.CacheManager;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.*;
import cn.com.api.service.*;
import cn.com.backstage.controller.base.U_cateBaseController;

import cn.com.util.*;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE)
public class U_cateBackController extends U_cateBaseController {

	private static Logger logger = Logger.getLogger(U_cateBackController.class);

	@Autowired
	private U_cateService u_cateService;
	@Autowired
	private StartOnLoadService startOnLoadService;
	@Autowired
	private U_article_cateService u_article_cateService;
	@Autowired
	private U_labelService u_labelService;

	/**
	 * 文章类别列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "article_cate_list")
	public ModelAndView cate_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/article_cate_list.jsp");
		// 分类列表
		List<U_cate> u_cates = startOnLoadService.article_cates();
		mv.addObject("u_cates", u_cates);
		return mv;
	}

	/**
	 * 文章类别列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "document_cate_list")
	public ModelAndView document_cate_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/document_cate_list.jsp");
		// 分类列表
		List<U_cate> aCates = u_cateService.findU_catesOrderByParentId(33);
		mv.addObject("u_cates", aCates);
		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "article_cate_add_pre")
	public ModelAndView article_cate_add_pre(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/article_cate_add.jsp");
		String parent_id = request.getParameter("parent_id");
		mv.addObject("parent_id", parent_id);
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			U_cate u_cate = u_cateService.findU_cateById(Integer.valueOf(id));
			mv.addObject("u_cate", u_cate);
		}
		// List<Ylj_All_Label_Obj> labels = u_labelService.findAllU_labels();
		// mv.addObject("labels", labels);

		// 文章类型 从数据字典中去
		// List<ApiT_enums> article_types = new ArrayList<ApiT_enums>();
		// List<ApiT_enums> enums = startOnLoadService.enum_dict();
		// String article_type_id = "0";
		// for (ApiT_enums the_enum : enums) {
		// if (the_enum.getItem_key().equalsIgnoreCase("article_type"))
		// article_type_id = the_enum.getId();
		// }
		// for (ApiT_enums the_enum : enums) {
		// if (the_enum.getParent_id().equalsIgnoreCase(article_type_id))
		// article_types.add(the_enum);
		// }
		// mv.addObject("article_types", article_types);
		//
		// // 分类列表
		// // List<U_cate> u_cates = u_cateService.findU_cates();
		// List<U_cate> u_cates = startOnLoadService.article_cates_sub();
		// // 带有文件分类级别的显示名
		// for (U_cate cate : u_cates) {
		// String cate_str = startOnLoadService.cates_str_OrderById(cate);
		// cate.setCate_full_name(cate_str);
		// }
		// mv.addObject("cates", u_cates);
		//
		return mv;
	}

	/**
	 * 新增或者编辑文章分类
	 * 
	 * @return
	 */
	@RequestMapping(value = "cate-add")
	public void cate_add(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ApiT_user_admin admin = (ApiT_user_admin) request.getSession().getAttribute("user_admin");
		// String parent_id = request.getParameter("parent_id");
		String cate_name = request.getParameter("cate_name");
		// String cate_type = request.getParameter("cate_type");
		// String keywords = request.getParameter("keywords");
		String cate_desc = request.getParameter("cate_desc");
		String sort = request.getParameter("sort");
		String parent_id = request.getParameter("parent_id");
		// String creater_id = request.getParameter("creater_id");
		// String modified_by = request.getParameter("modified_by");
		String allow_comment = request.getParameter("allow_comment");
		String is_valid = request.getParameter("is_valid");
		String is_show_date = request.getParameter("is_show_date");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			U_cate u_cate = new U_cate();
			u_cate.setParent_id(1);
			u_cate.setCate_name(cate_name);
			u_cate.setCate_type(0);
			// u_cate.setKeywords(keywords);
			u_cate.setCate_desc(cate_desc);
			u_cate.setSort(Integer.valueOf(sort));
			u_cate.setParent_id(Integer.valueOf(parent_id));
			// ApiT_user_admin admin = (ApiT_user_admin)
			// request.getSession().getAttribute("user_admin");
			// u_cate.setCreater_id(admin.getUser_id());
			u_cate.setAdd_time(HourGap.getNowTime());
			// u_cate.setModified_by(admin.getUser_id());
			u_cate.setModified_time(HourGap.getNowTime());

			u_cate.setAllow_comment(Integer.valueOf(allow_comment));
			u_cate.setIs_valid(Integer.valueOf(is_valid));
			u_cate.setIs_show_date(Integer.valueOf(is_show_date));

			u_cateService.insertU_cate(u_cate);
			// clear

		} else {// edit
			U_cate u_cate = u_cateService.findU_cateById(Integer.valueOf(id));
			// u_cate.setId(id);
			// u_cate.setParent_id(Integer.valueOf(parent_id));
			u_cate.setCate_name(cate_name);
			// u_cate.setCate_type(Integer.valueOf(cate_type));
			// u_cate.setKeywords(keywords);
			u_cate.setCate_desc(cate_desc);
			u_cate.setSort(Integer.valueOf(sort));
			// u_cate.setCreater_id(Utils.CurrentUser(request).getUser_id());
			// u_cate.setModified_by(admin.getUser_id());
			u_cate.setModified_time(HourGap.getNowTime());
			u_cate.setAllow_comment(Integer.valueOf(allow_comment));
			u_cate.setIs_valid(Integer.valueOf(is_valid));
			u_cate.setIs_show_date(Integer.valueOf(is_show_date));
			u_cateService.updateU_cate(u_cate);
		}
		startOnLoadService.article_cates_clear();
		map.put(Util.RESULT, Util.RESULT_RC_SUCCESS);
		map.put(Util.MESSAGE, "添加成功！");
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "cate-delete")
	public void cate_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");

		Map<String, Object> map = new HashMap<String, Object>();
		int ret_i = 0;

		List<U_article_cate> u_art_cates = u_article_cateService.findU_article_catesByCate_id(id);
		if (u_art_cates.size() <= 0) {
			// delete
			ret_i = u_cateService.deleteU_cate(Integer.valueOf(id));
			// clear cache
			startOnLoadService.article_cates_clear();
		}

		//
		if (ret_i > 0) {
			map.put(Util.RESULT, Util.RESULT_RC_SUCCESS);
			map.put(Util.MESSAGE, "删除成功！");
		} else {
			map.put(Util.RESULT, Util.RESULT_RC_CHECK_FAIL);
			map.put(Util.MESSAGE, "删除失败, 请先删除分类下文章!");
		}

		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 子分类
	 * 
	 * @return
	 */
	@RequestMapping(value = "cate_list_by_parent_id")
	public void cate_list_by_parent_id(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			U_cate u_cate = u_cateService.findU_cateById(Integer.valueOf(id));

			List<U_cate> u_cates = u_cateService.findU_catesByParent_id(id);
			// 带有文件分类级别的显示名
			for (U_cate cate : u_cates) {
				String cate_str = startOnLoadService.cates_str_OrderById(cate);
				// cate.setCate_full_name(cate_str);
				// 添加和分类匹配的 标签组
				String keywords = cate.getKeywords() == null ? "" : cate.getKeywords();
				List<Ylj_All_Label_Obj> labs = u_labelService.findLikeU_labels(keywords);
				cate.setU_labels(labs);
			}

			ReturnListObject<U_cate> ret_obj = new ReturnListObject<U_cate>("0000", "操作成功", u_cates);
			JsonUtil.printJson(response, JsonUtil.object2json(ret_obj));
		}
	}

	/**
	 * 分类的附属标签组
	 * 
	 * @return
	 */
	@RequestMapping(value = "cate_lables_by_parent_id")
	public void cate_lables_by_parent_id(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			U_cate u_cate = u_cateService.findU_cateById(Integer.valueOf(id));
			// 查找标签组
			String keywords = u_cate.getKeywords() == null ? "" : u_cate.getKeywords();
			List<Ylj_All_Label_Obj> labs = u_labelService.findLikeU_labels(keywords);
			u_cate.setU_labels(labs);
			// return
			ReturnSingleObject<U_cate> ret_obj = new ReturnSingleObject<U_cate>("0000", "操作成功", u_cate);
			JsonUtil.printJson(response, JsonUtil.object2json(ret_obj));
		}
	}

}
